/**
 * 
 */
package org.whereiswally.job.findwally;

import java.util.List;

import org.apache.log4j.Logger;
import org.jppf.server.protocol.JPPFTask;
import org.whereiswally.model.Wally;
import org.whereiswally.model.WallyFace;
import org.whereiswally.net.picturerepository.PictureRepository;
import org.whereiswally.recognition.FaceRecognition;
import org.whereiswally.recognition.imp.faint.FaintFaceRecognition;

/**
 * @author LGIAVEDO
 * 
 */
public class FindWallyJob extends JPPFTask {

    private static Logger logger = Logger.getLogger(FindWallyJob.class);
    private FaceRecognition fr = new FaintFaceRecognition();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
	PictureRepository prRepository = null;
	try {
	    logger.info("Iniciando proceso de procesamiento");
	    Wally wally = (Wally) getDataProvider().getValue(
		    URIDataProvider.WALLY);
	    // Intentamo obtener una uri para procesar
	    prRepository = (PictureRepository) getDataProvider().getValue(
		    URIDataProvider.NEXT_URI);
	    if (prRepository != null) {
		// Hay un nuevo PictureRepository
		logger.info("Procesando el PictureRepository: "
			+ prRepository.getUri());
		List<WallyFace> result = fr.tryRecognition(wally, prRepository
			.getAllPicturesOfRepository());

		setResult(result);

	    } else {
		// NO habia nada que procesas
		logger
			.warn("NO se ha encontrado ningun PictureRepository para procesar");
	    }

	} catch (Exception e) {
	    logger.error(e);
	}

    }
}
